var Web3 = require('web3')
var web3 = new Web3()


//单位转换
console.log('\n将wei转换为ether, Gwei, Mwei')
console.log(web3.utils.fromWei('12345567890876433', 'ether'))
console.log(web3.utils.fromWei('12345567890876433', 'Gwei'))
console.log(web3.utils.fromWei('12345567890876433', 'Mwei'))

console.log('\n转换为Wei')
console.log(web3.utils.toWei('1', 'ether'))
console.log(web3.utils.toWei('1', 'Gwei'))
console.log(web3.utils.toWei('1', 'Mwei'))



console.log("=== 进制转换成16进制 ===")
console.log(web3.utils.toHex('a'))
console.log(web3.utils.toHex(1234))
console.log(web3.utils.toHex({name:'Duke'}))

//将所有传入的数据都当做字符串进行处理，然后按照ASCII的16进制返回
//如果内部有单引号，则自动转化成双引号，再在外部用单引号括起来
console.log(JSON.stringify({name:'Duke'}))
console.log(web3.utils.toHex('{"name":"Duke"}'))

console.log(web3.utils.toHex(JSON.stringify({name:'Duke'})))

console.log(web3.utils.toHex([1,2,3,4]))
console.log(web3.utils.toHex('[1,2,3,4]'))


console.log("先将字符串'xyz'转换为ascii，然后转化为十六进制")
var str = web3.utils.fromAscii('xyz')
console.log(str) //0x78797a
var decimal = web3.utils.toDecimal(str)
console.log('转换成10进制为:')
console.log(decimal) //<--- 7895418

console.log("先将十六进制转换为ascii，然后转化为字符串")
str = web3.utils.toAscii('0x78797a')
console.log(str)


console.log('校验地址有效性:')
//这个函数不准确，慎用，只判断位数，不判读校验码
let validAddress   = '0x627306090abab3a6e1400e9345bc60c78a8bef57'
let inValidAddress = '0x627306090abab3a6e1400e9345bc60c78a8bef590'
console.log(web3.utils.isAddress(validAddress))
console.log(web3.utils.isAddress(inValidAddress))


var hash0 = web3.utils.sha3('abc')
console.log(hash0)

//对结果0x4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45进行hash
var hash1 = (web3.utils.sha3(hash0))
console.log(hash1)

//对结果4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45进行hash
//将‘0x’去除掉
console.log(web3.utils.sha3(hash0, {encoding:'hex'}))
